package com.hanscan.persistence.controllers;


import com.hanscan.persistence.EntityManagerSingleton;
import com.hanscan.persistence.controllers.exceptions.NonexistentEntityException;
import com.hanscan.persistence.entities.IdentificationFormat;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;


public class IdentificationFormatJpaController implements Serializable
{

    public IdentificationFormatJpaController()
    {
    }

    public EntityManager getEntityManager()
    {
        return EntityManagerSingleton.getInstance();
    }

    public void create(IdentificationFormat identificationFormat) throws Exception
    {
        //TODO: comprobaciones
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(identificationFormat);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void edit(IdentificationFormat identificationFormat) throws NonexistentEntityException,
            Exception
    {
        //TODO: comprobaciones
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            identificationFormat = em.merge(identificationFormat);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void destroy(IdentificationFormat identificationFormat) throws Exception
    {
        //TODO: comprobaciones
        //TODO: si se borra usuario, se borran tb sus localizaciones, a no ser q el borrado sea logico
        EntityManager em = null;
        try
        {
            em = getEntityManager();
            em.getTransaction().begin();
            em.remove(identificationFormat);
            em.getTransaction().commit();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public List<IdentificationFormat> findIdentificationFormatEntities()
    {
        return findIdentificationFormatEntities(true, -1, -1);
    }

    public List<IdentificationFormat> findIdentificationFormatEntities(
            int maxResults,
            int firstResult)
    {
        return findIdentificationFormatEntities(false, maxResults, firstResult);
    }

    private List<IdentificationFormat> findIdentificationFormatEntities(
            boolean all,
            int maxResults, int firstResult)
    {
        EntityManager em = getEntityManager();
        try
        {
            Query q =
                    em.createQuery(
                    "select object(o) from IdentificationFormat as o");
            if (!all)
            {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        }
        finally
        {
            em.close();
        }
    }

    public IdentificationFormat findIdentificationFormat(Long id) throws NonexistentEntityException
    {
        EntityManager em = getEntityManager();
        IdentificationFormat identificationFormat = null;
        em.clear();

        try
        {
            Query query = em.createNamedQuery("findIdentificationFormatById");
            query.setParameter(1, id);
            identificationFormat = (IdentificationFormat) query.getSingleResult();
            return identificationFormat;

        }
        catch (NoResultException ex)
        {
            throw new NonexistentEntityException(ex.getMessage());
        }
    }

    public int getIdentificationFormatCount()
    {
        EntityManager em = getEntityManager();
        try
        {
            Query q =
                    em.createQuery("select count(o) from IdentificationFormat as o");
            return ((Long) q.getSingleResult()).intValue();
        }
        finally
        {
            em.close();
        }
    }
}
